#!/usr/loca/bin/python

#-*- encoding:utf-8 -*-

import utils
import heapq
import sys
K = 5000;
if __name__ == '__main__':
    result  = open(sys.argv[1], 'w')
    W = utils.load_sparse_csc('W_S.npz');
    N = W.transpose();
    print type(N);
    print N.todense();
    h = [];
        
    r = 0;
    while(r < N.shape[0]):
        row_data = N.data[N.indptr[r]:N.indptr[r+1]];
        row_index = N.indices[N.indptr[r]:N.indptr[r+1]];
        row_sum = sum(row_data);
        if row_sum == 0.0:
            r += 1;
            continue;
        else:
            for pos, d in enumerate(row_data):
                if r != row_index[pos] and d != 0.0:
                    heapq.heappush(h, (-d*d/row_sum, row_index[pos], r));
        r += 1;

    li = [heapq.heappop(h) for k in range(min(K, len(h)))];
    su = max([-s[0] for s in li]);

    for l in li:
        result.write('%d\t%d\t%f\n' %(l[2], l[1], -l[0]));
        #print('%d\t%d\t%f\n' %(l[2], l[1], -l[0]));


    result.close();




